<?php
/***
 * init.php - admin directory initialization
 * Copyright (c) 2005-2007, BidiX (http://tiddlyhome.bidix.info/#License)
 *
 * source : http://tiddlyhome.bidix.info/#Source
 * usage : init.php
 *
 ***/


require_once('../lib/Htpasswd.php');
require_once('../lib/Htaccess.php');
require_once('../lib/Htgroup.php');


function displayHeader() {
$pageTitle = "Admin directory initialiation";
require("../lib/pageHeader.php");
}
 
function displayForm() {
?>
	<script type="text/javascript">
	function checkAndSubmit() {
		
		var the_form = document.getElementById('the_form');
		var passwordField = document.getElementById('password');
		var passwordCheckField = document.getElementById('password_check');
		var emailField = document.getElementById('email');
		var password = passwordField.value;
		var password_check = passwordCheckField.value;
		var email = emailField.value;
		if (password.length < 5)
			alert('Your password must be at least five characters. Please enter a longer password.');
		else if (password != password_check)
			alert('Your password was confirmed incorrectly. Please try again.');
		else if (email.length < 3)
				alert('Your email is required.');
		else
			the_form.submit();
	}
	</script>
	<form id="the_form" method="POST" action="">
		<table>
			<td>Admin password<br><small>(at least 5 characters)</small></td>
			<td><input type="password" name="password" id="password"></td>
		</tr>
		<tr>
			<td>confirm Admin password</td>
			<td><input type="password" name="password_check" id="password_check"></td>
		</tr>
		<tr>
			<td>Admin email</td>
			<td><input type="text" name="email" id="email"></td>
		</tr>
		<tr>
			<td></td>
			<td class="button"><b><a href="javascript:checkAndSubmit();" class="button">Init Admin &#187;</a></b><br/>
			</td>
		</tr>
	</table>
	</form>
	</center>
<?php
}


function displayFooter() {
require("../lib/pageFooter.php");
}


function processForm() {
	$user = "Admin";
	$HTPASSWD_FILENAME = '.htpasswd';
	$HTGROUP_FILENAME = '.htgroup';
	$HTACCESS_FILENAME = '.htaccess';
	$ROOT = dirname(dirname(dirname(__FILE__))).'/';
	$TH_DIR = $ROOT . '_th/';
	$ADMIN_DIR = $TH_DIR.'admin';
	$LIB_DIR = $TH_DIR.'lib';
	$TEMPLATE_DIR = $TH_DIR.'templates';
	
	$password = $_REQUEST['password'];
	$email = $_REQUEST['email'];

	// init .htpasswd
	$users = new Htpasswd($HTPASSWD_FILENAME);
	$users->setUser($user,$password,$email);
	$users->save();
	if (!is_file($HTPASSWD_FILENAME)) {
		die ("$HTPASSWD_FILENAME not created.");
	}
	echo ("<p>$HTPASSWD_FILENAME created.</p>");

	// init .htgroup
	$groups = new Htgroup($HTGROUP_FILENAME);
	$groups->setGroup($user,$user);
	$groups->save();
	if (!is_file($HTGROUP_FILENAME)) {
		die ("$HTGROUP_FILENAME not created.");
	}
	echo ("<p>$HTGROUP_FILENAME created.</p>");

	//init .htaccess
	$dir = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME']);
	$access = new Htaccess($HTACCESS_FILENAME);
	$access->content['AuthType'] = 'Basic';
	$access->content['AuthName'] = '"TiddlyHome '.$user.'"';
	$access->content['AuthUserFile'] = $dir . "/" . "$HTPASSWD_FILENAME";
	$access->content['Require'] = "user $user";
	$access->save();
	if (!is_file($HTACCESS_FILENAME)) {
		die ("$HTACCESS_FILENAME not created.");
	}
	echo ("<p>$HTACCESS_FILENAME created.</p>");
	
	//init lib/config.php
	($f = fopen("$LIB_DIR/config.php",w)) || die("Can't create '$LIB_DIR/config.php'");
	fwrite($f,"<?php\n\n");
	fwrite($f,"// config.php - generated by TiddlyHome init.php\n\n");
	fwrite($f,'$ROOT = \'' . $ROOT . "';\n");
	fwrite($f,'$TH_DIR = \'' . $TH_DIR . "';\n");
	fwrite($f,'$HTPASSWD_FILENAME = \'' . $HTPASSWD_FILENAME . "';\n");
	fwrite($f,'$HTACCESS_FILENAME = \'' . $HTACCESS_FILENAME . "';\n");
	fwrite($f,'$HTGROUP_FILENAME = \'' . $HTGROUP_FILENAME . "';\n");
	fwrite($f,'$ADMIN_DIR = \'' . $ADMIN_DIR . "';\n");
	fwrite($f,'$LIB_DIR = \'' . $LIB_DIR . "';\n");
	fwrite($f,'$TEMPLATE_DIR = \'' . $TEMPLATE_DIR . "';\n");
	if ($_SERVER['HTTPS'])
		$protocol = 'https';
	else
		$protocol = 'http';
	$port = '';
	if ($_SERVER['SERVER_PORT'] != 80)
		$port = ':'.$_SERVER['SERVER_PORT'];
	$root = dirname(dirname(dirname($_SERVER['REQUEST_URI'])));
	if ($root != '/')
		$root = $root . '/';
	$url = $protocol."://".$_SERVER['HTTP_HOST'].$port.$root;
	fwrite($f,'$ROOT_URL = \''.$url ."';\n");
	fwrite($f,'$ROOT_URN = \''.$root ."';\n");
	fwrite($f,"\n\n?>\n");
	fclose($f);
	echo ("<p>config.php created.</p>");
	
}


function main() {
	if (is_file('.htaccess')) {
		displayHeader();
		echo "<p>TiddlyHome Admin already initialize !</p>";
		displayFooter();
		exit;
	}
	if ($_SERVER['REQUEST_METHOD'] == 'GET') {
		displayHeader();
		displayForm();
		displayFooter();
		exit;
	}
	// POST
	displayHeader();
	processForm();
	displayFooter();
}


main();
?>


